package com.topone.controller;


import com.topone.Exception.FanZoneServiceException;
import com.topone.Utils.ResultVOUntil;
import com.topone.VO.ResultVO;
import com.topone.controller.aspect.TcUserAspect;
import com.topone.dataObject.TcUser;
import com.topone.service.TcUserService;
import com.topone.sms.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/buyer")
@Slf4j
public class SmsController {

    @Autowired
    private SmsService smsSohuService;
    @Autowired
    private TcUserService tcUserService;

    @GetMapping("/sms/{mobile}/{openid}")
    public ResultVO sms(@PathVariable String mobile, @PathVariable String openid) {

        try {
                  boolean backdoor = mobile.startsWith("99");
                  String code = smsSohuService.sendSmsCode(mobile, backdoor);
                  if (backdoor) {
                      return ResultVOUntil.success(String.format("{\"code\":\"%s\"}", code));
                  }
                  return ResultVOUntil.success(String.format("{\"code\":\"%s\"}", "发送成功"));
        }catch (FanZoneServiceException e){
            log.info("修改手机号码 发送短信失败:",e.getMessage());
            throw new FanZoneServiceException("短信发送失败");
        }
    }

    @GetMapping("/sms/remind")
    public ResponseEntity remind(@RequestParam(value = "userPhone", required = false) String userPhone,
                                 @RequestParam(value = "couponName") String couponName,
                                 @RequestParam(value = "userName") String userName,
                                 @RequestParam(value = "businessName")String businessName) {
        boolean backdoor = userPhone.startsWith("99");
        smsSohuService.sendCheckCouponSmsCode(userPhone,userName, couponName, businessName, backdoor);
        if (backdoor) {

            return ResponseEntity.ok(String.format("发送成功"));
        }
        return ResponseEntity.ok(String.format("发送成功"));

    }

}
